home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / ACTLIB.ZIP / TOOLS.ZIP / FILE.C < prev    next >
C/C++ Source or Header  |  1993-06-25  |  7KB  |  263 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. /*
  4.  * File         : file.c
  5.  *
  6.  * Functions    : filencopy
  7.  *          Filencopy
  8.  *                filetrunc
  9.  *          Filetrunc
  10.  *          Filecat
  11.  *
  12.  * Description  : File management functions.
  13.  *
  14.  * Decisions    :
  15.  *
  16.  */
  17.  
  18.  
  19. #include "strings.h"
  20. #include "tools.h"
  21. #include <malloc.h>
  22. #include <io.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>  /* because used by stat.h in Microsoft */
  25. #include <sys/stat.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29.  
  30. #define BUFSIZE         1024L     /*  Buffer size : to be optimized        */
  31.                 /*  Very context dependant !!            */
  32.                 /*  Under Sun 3.0 : 1 page = 8192 bytes  */
  33.                 /*  Under MS-DOS  : 1 page =  512 bytes  */
  34.  
  35. #define PERMISSION    0666    /*  RW for owner, R for everybody else  */
  36.  
  37.  
  38.  
  39. /***
  40.  *  Function    :  filencopy
  41.  *
  42.  *  Description :  Copy n characters from a file on another.
  43.  *
  44.  *  Parameters  :  in   int            target    target file descriptor
  45.  *                 in   int            source    source file descriptor
  46.  *                 in   unsigned long  length    number of bytes to copy
  47.  *
  48.  *  Precond     :  The two files must be opened.
  49.  *
  50.  *  Decisions   :  The copy can stop when eof is reached.
  51.  *
  52.  *  Warning     :  no rewind is performed on either file!!!
  53.  *
  54.  *  Return      :   0 if OK
  55.  *                 errno if set
  56.  *                 -1 otherwise
  57.  *
  58.  *  OS/Compiler :  All
  59.  ***/
  60.  
  61. int filencopy( int source, int target, unsigned long length )
  62. {
  63.  int nread;
  64.  unsigned bufsize;
  65.  char *buffer;
  66.  
  67.  /* Build buffer as big as possible */
  68.  for ( bufsize = min(0x7FFF, length); bufsize >= 128; bufsize /= 2 )
  69.      if ( buffer = (char *) malloc(bufsize) ) break;
  70.  if ( bufsize < 128 ) return -1;
  71.  
  72.  while ( nread = read(source, buffer, (unsigned)min(length, (unsigned long)bufsize)) )
  73.     {
  74.      if ( nread == -1 || nread != write(target, buffer, nread) )
  75.         {
  76.          free( buffer );
  77.          if ( errno ) return errno;
  78.                  else return -1;
  79.         }
  80.  
  81.      if ( (nread == length) || eof(source) ) break;
  82.  
  83.      length -= nread;
  84.     }
  85.  
  86.  free( buffer );
  87.  return 0;
  88. }
  89.  
  90. /***
  91.  *  Function    :  Filencopy
  92.  *
  93.  *  Description :  Copy n characters from a file on another.
  94.  *
  95.  *  Parameters  :  in   char *         source    source filename
  96.  *                 in   char *         target    target filename
  97.  *                 in   unsigned long  length    number of bytes to copy
  98.  *
  99.  *  Decisions   :  The copy can stop when eof is reached.
  100.  *
  101.  *  Return      :  0 if OK
  102.  *                 errno if set
  103.  *                 -1 otherwise
  104.  *
  105.  *  OS/Compiler :  All
  106.  ***/
  107.  
  108. int Filencopy( const char *source, const char *target, unsigned long length )
  109. {
  110.  int targetfile, sourcefile;
  111.  int error;
  112.  char *ptr1, targetname[_MAX_PATH];
  113.  
  114.  if ( strcmp(target, source) == 0 ) return Filetrunc(source, length);
  115.  
  116.  strcpy( targetname, target );
  117.  if ( *(ptr1 = strend(targetname) - 1) == '\\' )
  118.     {
  119.      const char *ptr2 = max( source, strrchr(source, '\\') + 1 );
  120.      strcpy( ++ptr1, ptr2 );
  121.     }
  122.  
  123.  /* change the default file mode from text to binary */
  124.  _fmode = O_BINARY;
  125.  if ( (sourcefile = open(source, O_RDONLY | O_BINARY)) == -1 ||
  126.       (targetfile = creat(targetname, S_IWRITE)) == -1
  127.     ) return errno;
  128.  
  129.  if ( error = filencopy(sourcefile, targetfile, length) ) return error;
  130.  
  131.  if ( (close(targetfile) == -1) || (close(sourcefile) == -1) ) return errno;
  132.  
  133.  return 0;
  134. }
  135.  
  136.  
  137.  
  138. /***
  139.  *  Function    :  Filecat
  140.  *
  141.  *  Description :  Concatenate two files on another.
  142.  *
  143.  *  Parameters  :  in   char *     newpath     result filename
  144.  *                 in   char *     path1       path1 filename
  145.  *                 in   char *     path2       path2 filename
  146.  *
  147.  *  Return      :  0 if OK
  148.  *                 errno if set
  149.  *                 -1 otherwise
  150.  *
  151.  *  OS/Compiler :  All
  152.  ***/
  153.  
  154. int Filecat( const char *newpath, const char *path1, const char *path2 )
  155. {
  156.  int fd1, fd2, newfd;
  157.  int error;
  158.  
  159.  if ( strcmp(path2, newpath) ) return -1; /*  new path equal to second one  */
  160.  
  161.  if ( (fd2 = open(path2, O_RDONLY | O_BINARY)) == -1 ) return errno;
  162.  
  163.  /* change the default file mode from text to binary */
  164.  _fmode = O_BINARY;
  165.  
  166.  if ( strcmp(path1, newpath) )
  167.     {                /*  new path different than first one  */
  168.      if ( (fd1 = open(path1, O_RDONLY | O_BINARY)) == -1 ||
  169.           (newfd = creat(newpath, S_IWRITE)) == -1
  170.         ) return errno;
  171.      if ( error = filecopy(fd1, newfd) ) return error;
  172.      if ( close(fd1) == -1) return errno;
  173.     }
  174.  else if ( (newfd = open(path1, O_APPEND | O_BINARY)) == -1 ) return errno;
  175.  
  176.  if ( error = filecopy(newfd, fd2) ) return error;
  177.  
  178.  if ( (close(fd2) == -1) || (close(newfd) == -1) ) return errno;
  179.  
  180.  return 0;
  181. }
  182.  
  183. /***
  184.  *  Function    :  filetrunc
  185.  *
  186.  *  Description :  Truncate a file to a specified length.
  187.  *
  188.  *  Parameters  :  in   int            fd        file descriptor
  189.  *                 in   unsigned long  length    number of bytes to copy
  190.  *
  191.  *  Precond     :  The file must be opened with writing permission
  192.  *
  193.  *  Warning     :  no rewind is performed on the file!!!
  194.  *
  195.  *  Return      :  0 if OK
  196.  *                 errno if error
  197.  *
  198.  *  OS/Compiler :  All
  199.  ***/
  200.  
  201. int filetrunc( int fd, unsigned long length )
  202.  
  203. {
  204. #if 0 
  205.  int nread;
  206.  unsigned bufsize;
  207.  char *buffer;
  208.  
  209.  /* Build buffer as big as possible */
  210.  for ( bufsize = min(0x7FFF, length); bufsize >= 128; bufsize /= 2 )
  211.      if ( buffer = (char *) malloc(bufsize) ) break;
  212.  if ( bufsize < 128 ) return -1;
  213.  
  214.  while ( nread = read(fd, buffer, min(BUFSIZE,length)) )
  215.     {
  216.      if ( nread == -1 )
  217.         {
  218.          free( buffer );
  219.          return errno;
  220.         }
  221.      length -= nread;
  222.     }
  223.  
  224.  nread = write( fd, buffer, 0 );
  225.  
  226.  free( buffer );
  227.  if ( nread == -1 ) return errno;
  228. #endif
  229.  
  230.  if ( chsize(fd, length) ) return errno;
  231.  return 0;
  232. }
  233.  
  234. /***
  235.  *  Function    :  Filetrunc
  236.  *
  237.  *  Description :  Truncate a file to a specified length.
  238.  *
  239.  *  Parameters  :  in   char *         path      file descriptor
  240.  *                 in   unsigned long  length    number of bytes to copy
  241.  *
  242.  *  Return      :  0 if OK
  243.  *                 errno if error
  244.  *
  245.  *  OS/Compiler :  All
  246.  ***/
  247.  
  248. int Filetrunc( const char *path, unsigned long length )
  249. {
  250.  int fd, error;
  251.  
  252.  /* change the default file mode from text to binary */
  253.  _fmode = O_BINARY;
  254.  
  255.  if ( (fd = creat(path, S_IWRITE)) == -1 ) return errno;
  256.  
  257.  if ( error = filetrunc( fd, length ) ) return error;
  258.  
  259.  if ( close(fd) == -1 ) return errno;
  260.  
  261.  return 0;
  262. }
  263.